<template>
  <div id="manageQuestion">
    <a-table
      :columns="columns"
      :data="list"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.currentPage,
        showTotal: true,
        total,
      }"
      @page-change="doPageChange"
    >
      <template #optional="{ record }">
        <a-button style="float: left" status="success" @click="doUpdate(record)"
          >修改
        </a-button>
        <a-button style="float: left" status="danger" @click="doDelete(record)"
          >删除
        </a-button>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { QuestionControllerService } from "../../../generated";
import { Message, Modal } from "@arco-design/web-vue";
import { useRouter } from "vue-router";

const searchParams = ref({
  pageSize: 2,
  currentPage: 1,
});

const doPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    currentPage: page,
  };
};

const router = useRouter();

const doUpdate = (t: any) => {
  router.push({
    path: "/update/question",
    query: {
      questionId: t.id,
    },
  });
};

let list = ref([
  {
    title: "",
    content: "",
    tags: [],
    answer: "",
    judgeCase: [
      {
        inputCase: "",
        outputCase: "",
      },
    ],
    judgeConfig: {
      timeLimit: "",
      stackLimit: "",
      memoryLimit: "",
    },
  },
]);

let total = ref(0);

const columns = [
  {
    title: "编号",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "标签列表",
    dataIndex: "tags",
  },
  {
    title: "题目内容",
    dataIndex: "content",
  },
  {
    title: "题目答案",
    dataIndex: "answer",
  },
  {
    title: "题目提交数",
    dataIndex: "submitNum",
  },
  {
    title: "题目通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "判题配置",
    dataIndex: "judgeConfig",
  },
  {
    title: "创建人",
    dataIndex: "user.userName",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

let questionQuery = ref({});

const getAllData = async () => {
  let res = await QuestionControllerService.listQuestionByPageUsingPost(
    questionQuery as any
  );
  if (res.code === 0) {
    total.value = res.data.total;
    list.value = res.data.records.map((t: any) => {
      t.judgeConfig = JSON.stringify(t.judgeConfig);
      return t;
    });
  } else {
    Message.error("获取失败：" + res.message);
  }
};

onMounted(async () => {
  getAllData();
});

const doDelete = async (t: any) => {
  let res = await QuestionControllerService.deleteQuestionUsingPost({
    id: t.id,
  });
  if (res.code === 0) {
    Message.success("删除成功");
    getAllData();
  } else {
    Message.error("删除失败：" + res.message);
  }
};
</script>

<style scoped>
#manageQuestion {
  padding: 20px;
}
</style>
